<?php 
//Recepción de parametros iniciales
$mercadoVial 	   = 0;
$mercadoTransporte = 0;
$mercadoGruas 	   = 0;
$mercadoAgricola   = 0;
$mercadoServicios  = 0;
$limit			   = 0;
$email			   = '';
$diasAntes		   = 0;
$mercadoVial 	   = $_REQUEST["vial"];
$mercadoTransporte = $_REQUEST["trans"];
$mercadoGruas 	   = $_REQUEST["grua"];
$mercadoAgricola   = $_REQUEST["agro"];
$mercadoServicios  = $_REQUEST["serv"];
$limit			   = $_REQUEST["avisos_x_cat"];
$email			   = $_REQUEST["email"];
$diasAntes		   = $_REQUEST["dias_antes"];
/////////Fin Parametros//////////////
require("adm_header.php"); 
ini_set("max_execution_time","1000");
///////////////////////////////////////
/////////////////Caso de Prueba de Email ///////////////

if($_POST['sendmail']){

	/*$arrayOfUser = explode(",", $_POST['userEmail']);		
	foreach ($arrayOfUser as $userEmail){*/
		sendEmail(trim($_POST['userEmail']),$set_webmaster_address,$_POST['subject'],$_POST['newsCode'], '',true,true);		
	//}
}

//////////////////////////////////////////////////////
if(!empty($email)){
	$record 		   = getUserFromNewsletterTable($email);
	$mercadoVial 	   = $record['mercado_vial'];
	$mercadoTransporte = $record['mercado_transporte'];
	$mercadoGruas 	   = $record['mercado_autoelevadores'];
	$mercadoAgricola   = $record['mercado_agricola'];
	$mercadoServicios  = $record['servicios'];
}else{

/**********************************************/
	$arrayCondiciones = array();
	if($mercadoVial)
		$arrayCondiciones[] = "mercado_vial = 1";
	if($mercadoTransporte)
		$arrayCondiciones[] = "mercado_transporte = 1";
	if($mercadoGruas)
		$arrayCondiciones[] = "mercado_autoelevadores = 1";
	if($mercadoAgricola)
		$arrayCondiciones[] = "mercado_agricola = 1";
	if($mercadoServicios)
		$arrayCondiciones[] = "servicios = 1";
	
	$users = getUsersFromNewsletterTableWhere($arrayCondiciones,array( 0 => 'email asc'), 0, 0);
}
$baseUrl = "http://".$set_url;
$post = array();
$get = "";
if($mercadoVial){
		$post['vial'] = "1";
		$get.='?vial=1';
}else{
		$get.='?vial=0';
}
if($mercadoTransporte){
		$post['trans'] = "1";
		$get.='&trans=1';
}
if($mercadoGruas){
		$post['grua'] = "1";
		$get.='&grua=1';
}
if($mercadoAgricola){
		$post["agro"] = "1";
		$get.='&agro=1';
}
if($mercadoServicios){
		$post['serv'] = "1";
		$get.='&serv=1';
}
if(!empty($limit)){
		$post['limit'] = $limit;
		$get.='&limit='.$limit;
}
if(!empty($diasAntes)){
		$post['days'] = $diasAntes;
		$get.='&days='.$diasAntes;
}		

?>
<? writeCommonAdminTable(LA_ADMIN_NEWSL_TITLE,1); ?>
<!-- ********************************************************** -->
<?php 

?>
<form action="adm_newsletter3.php" method="post">
<table>
<tbody>    
  <tr>
    <td colspan="2"><b>Usuarios:</b></td>    
  </tr>
  <tr>
  		<td colspan="2"">
		    <textarea name="userEmail" rows="10" cols="85" readonly="readonly" style="overflow:auto;"><?php 
		    	if(!empty($email)) echo $email;
		    	else{
		    		$first = true;
		    		foreach ($users as $user){		    			
		    			if($first) $first=false;
		    			else echo ", ";
		    			echo $user['email'];
		    		}		    		
		    	} 		    	
		    ?></textarea><br />
		    <a href='<?="adm_newsletter3.csv.php?vial={$mercadoVial}&trans={$mercadoTransporte}&grua={$mercadoGrua}&agro={$mercadoAgricola}&serv={$mercadoServicios}"?>'>Bajar direcciones de correo a CSV</a>	    
  		</td>
  </tr>
  <tr>
    <td colspan="2"><b>Newsletter Code:</b></td>    
  </tr>
  <tr>
  		<td colspan="2"">
		    
		     <textarea name="newsCode" rows="10" cols="85" readonly="readonly" style="overflow:auto;"><?php 
			     $context = array();
				 ksort($post);	
				 $context['http'] = array
				(
					'method' => 'POST',
					'content' => http_build_query($post, '', '&'),
				);		
				echo  file_get_contents($baseUrl.'/newsletter.php', false, stream_context_create($context));		     
		     ?></textarea>		    
  		</td>
  </tr>
  <tr>
  	<td><a href="adm_newsletter2.php" >Volver</a></td>
    <td><a target="_blank" href="<?php echo $baseUrl.'/newsletter.php'.$get ?>" >Newsletter Preview</a></td>    
  </tr>
  <tr>
  	<td colspan="2">&nbsp;</td>        
  </tr>  
  <tr>
  	<td colspan="2"><b>Prueba:</b></td>
  </tr>
  <tr>
  	<td>Asunto: <input type="text" name="subject" value="Newsletter - MercadoVial"/></td>	
  	<td>
	  	<input type="hidden" value="<?php echo $mercadoVial ?>"         name="vial" />
		<input type="hidden" value="<?php echo $mercadoTransporte ?>"   name="trans" />
		<input type="hidden" value="<?php echo $mercadoGruas ?>" 	    name="grua" />
		<input type="hidden" value="<?php echo $mercadoAgricola ?>"     name="agro" />
		<input type="hidden" value="<?php echo $mercadoServicios ?>"    name="serv" />
		<input type="hidden" value="<?php echo $limit ?>"			    name="avisos_x_cat" />
		<input type="hidden" value="<?php echo $email ?>"			    name="email" />
		<input type="hidden" value="<?php echo $diasAntes ?>"		    name="dias_antes" />
	  	<input type="submit" name="sendmail" value="Enviar" />
  	</td>        
  </tr>
   
</tbody>  
</table>
</form>
<!-- ********************************************************** -->
<? writeCommonAdminTable("Email Users",""); ?><!-- END Table menu -->
<? require("adm_footer.php"); ?>